package android.cs2340UDT;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
;

public class ListViewActivity extends Activity implements OnClickListener {
	 /** Called when the activity is first created. */
	
	Manager manage = Security.getManager();
	private ToDoList currentList;
	private static ArrayList<Item> tempList = new ArrayList<Item>();
	private static String tempCategory = "";
	
	public static ArrayList<Item> getTempList()    {
    	return tempList;
    }
	
	public static void setTempList(ArrayList<Item> newList)    {
    	tempList = newList;
    }
	
	public static String getTempCategory()	{
		return tempCategory;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
    
        ImageButton BackButton = (ImageButton) findViewById(R.id.Back);
        ImageButton SettingsButton = (ImageButton) findViewById(R.id.ListViewSettings);
        ImageButton AddButton = (ImageButton) findViewById(R.id.Add);
        
        Button MapButton = (Button) findViewById(R.id.Map);
        Button ListsButton = (Button) findViewById(R.id.Lists);
        Button CalButton = (Button) findViewById(R.id.Cal);
        
        // Gets the linear layout to add item buttons to
        LinearLayout itemList = (LinearLayout) findViewById(R.id.linearLayout1);
        
        // Gets the text view to input list name into
        TextView listName = (TextView) findViewById(R.id.ListName);
        
        // Retrieves the current list in use from manager
        currentList = manage.getCurrentItemList();
        
        // Sets the UI list name view to current list's name
        listName.setText(currentList.getName());
        
        // Gets the list of items from the current ToDoList
        if(currentList.sortString.equals("All Items")) {
        	tempList = currentList.getList();
        }
        else if (currentList.sortString.equals("Completed")){
        	tempList = currentList.showComplete();
        }
        else if (currentList.sortString == "Not Completed"){
        	tempList = currentList.showUnComplete();
        }
        else if (currentList.sortString == "Before Date"){
        	tempList = currentList.showDate(0);
        }
        else if (currentList.sortString == "After Date"){
        	tempList = currentList.showDate(1);
        }
        
        // For each item, create a button with its name, set its action listener, and add the button to the UI
        for(Item item : tempList) {
        	Button itemButton = new Button(this);
        	itemButton.setText(item.getDescription());
        	itemButton.setOnClickListener(this);
        	itemList.addView(itemButton);
        }
        
        
        //Listening to Register Add item Button button event
        AddButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View arg0) {
        		//Starting a new Intent
        		Intent nextScreen = new Intent(getApplicationContext(), CreateItemActivity.class);
        		finish();
        		//Switches over to CreateItemActivity
        		startActivity(nextScreen);

        	}
        });
        
        BackButton.setOnClickListener(new View.OnClickListener() {
        	
        	public void onClick(View arg0) 	{
        		Intent nextScreen = new Intent(getApplicationContext(), MainScreenActivity.class);
        		finish();
        		startActivity(nextScreen);
        	}
			
		});
        
        SettingsButton.setOnClickListener(new View.OnClickListener() {
        	
        	public void onClick(View arg0) {
        		Intent nextScreen = new Intent(getApplicationContext(), ListSettingsView.class);
        		finish();
        		startActivity(nextScreen);
        	}
			
		});
        
        MapButton.setOnClickListener(new View.OnClickListener() {
        	
        	public void onClick(View arg0) {
        		Intent nextScreen = new Intent(getApplicationContext(), MapScreenActivity.class);
        		startActivity(nextScreen);
        	}
			
		});

    
        class MyOnItemSelectedListener implements OnItemSelectedListener{
        	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        		if(parent.getItemAtPosition(pos).toString() == "Uncompleted"){
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = currentList.showUnComplete();
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        		
        		else if(parent.getItemAtPosition(pos).toString() == "All Items"){
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = manage.getToDoList().get(j).getList();
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        		else if(parent.getItemAtPosition(pos).toString() == "Personal"){
        			tempCategory = "Personal";
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = manage.getToDoList().get(j).showCategory(manage.getToDoList().get(j).getList(), "Personal");
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        		else if(parent.getItemAtPosition(pos).toString() == "School"){
        			tempCategory = "School";
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = manage.getToDoList().get(j).showCategory(manage.getToDoList().get(j).getList(), "School");
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        		else if(parent.getItemAtPosition(pos).toString() == "Work"){
        			tempCategory = "Work";
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = manage.getToDoList().get(j).showCategory(manage.getToDoList().get(j).getList(), "Work");
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        		else if(parent.getItemAtPosition(pos).toString() == "Date"){
        			for(int j = 0; j < manage.getToDoList().size(); j++){
        				tempList = manage.getToDoList().get(j).showDate(0);
        				for(int i = 0; i < tempList.size(); i++){
        					Toast.makeText(parent.getContext(), tempList.get(i).getDescription(), Toast.LENGTH_LONG).show();
        				}
        			}
        		}
        	}
        	public void onNothingSelected(AdapterView parent){
            		//Do nothing
            }
        }
        
        
    }
    
    // This method is passed into newly created List buttons with the this attribute. Sets their action listener to move to ItemViewActivity
    public void onClick(View view) {
    	//Starting a new Intent
		Intent nextScreen = new Intent(getApplicationContext(), ItemViewActivity.class);
		manage.setCurrentItem(((Button)view).getText().toString());
		finish();
		//Switches over to ItemViewActivity
		startActivity(nextScreen);
     }
    
}
